
using System.Threading;
using UnityEngine;
using System;
public class CCanvas {
	

    
	public const sbyte LEFT=4;
	public const sbyte  RIGHT=6;
	public const sbyte  UP=2;
	public const sbyte  DOWN=8;
	public const sbyte  ENTER=8;
	
	public static MyRandom r = new MyRandom();
	public static bool isRunning = true;
	public static int delay = 40;
	public static CCanvas instance;
	public static GameScr gameScr;
	public static CScreen curScr;
	
	Thread thread;
	static bool[] keyHold = new bool[14];
	public static bool[] keyPressedz = new bool[14];
	public static bool isPointerDown;
	public static bool isPointerClick;
	public static bool isPointerJustRelease;
	public static bool isTouch = false; // ---> MÃƒÂ n hÃƒÂ¬nh cÃ¡ÂºÂ£m Ã¡Â»Â©ng
	public static bool isTouchControl; // ----> Ã„Â�iÃ¡Â»Â�u khiÃ¡Â»Æ’n bÃ¡ÂºÂ±ng cÃ¡ÂºÂ£m Ã¡Â»Â©ng
	public static bool isTouchControlSmallScreen; // ----> Ã„Â�iÃ¡Â»Â�u khiÃ¡Â»Æ’n bÃ¡ÂºÂ±ng cÃ¡ÂºÂ£m Ã¡Â»Â©ng mÃƒÂ n hÃƒÂ¬nh nhÃ¡Â»Â�
	public static bool isTouchControlLargeScreen; // ----> Ã„Â�iÃ¡Â»Â�u khiÃ¡Â»Æ’n bÃ¡ÂºÂ±ng cÃ¡ÂºÂ£m Ã¡Â»Â©ng mÃƒÂ n hÃƒÂ¬nh lÃ¡Â»â€ºn
	public static bool isPointerJustDown;
	public static int px, py, pxFirst, pyFirst, pxLast, pyLast;
	public static int w, h, hw, hh, wd3, hd3, w2d3, h2d3, w3d4, h3d4, wd6, hd6;
	public CCanvas(){

        instance = this;
        w = (int)ScaleGUI.WIDTH;
        h = (int)ScaleGUI.HEIGHT;
     
		instance = this;
		
		gameScr = new GameScr();
		gameScr.show();
		
	}
	public void mapKeyPress(int keyCode) {

		switch (keyCode) {
		case -1:
			keyHold[2] = true;
			keyPressedz[2] = true;
			return;
		case -2:
			keyHold[8] = true;
			keyPressedz[8] = true;
			return;
		case -3:
			keyHold[4] = true;
			keyPressedz[4] = true;
			return;
		case -4:
			keyHold[6] = true;
			keyPressedz[6] = true;
			return;
		case -5:
		case 10:
			keyHold[5] = true;
			keyPressedz[5] = true;
			return; // [i]
		case 48:
		case 49:
		case 50:
		case 51:
		case 52:
		case 53:
		case 54:
		case 55:
		case 56:
		case 57:
			
			keyHold[keyCode - 48] = true;
			keyPressedz[keyCode - 48] = true;
		
			return; // 0 -> 9
		case 42:
			keyHold[10] = true;
			keyPressedz[10] = true;
			return; // Key [*]
		case 35:
			keyHold[11] = true;
			keyPressedz[11] = true;
			return; // Key [#]
		case -6:
		case -21:
			keyHold[12] = true;
			keyPressedz[12] = true;
			return; // Soft1
		case -7:
		case -22:
			keyHold[13] = true;
			keyPressedz[13] = true;
			return; // Soft2
		}

	}
	//xÃ¡Â»Â­ lÃƒÂ½ sÃ¡Â»Â± kiÃ¡Â»â€¡n phim duy chuyÃ¡Â»â€¦n trong game
	public void mapKeyRelease(int keyCode) {
		switch (keyCode) {
		case -1:
			keyHold[2] = false;
			keyPressedz[2] = false;
			return;
		case -2:
			keyHold[8] = false;
			keyPressedz[8] = false;
			return;
		case -3:
			keyHold[4] = false;
			keyPressedz[4] = false;
			return;
		case -4:
			keyHold[6] = false;
			keyPressedz[6] = false;
			return;
		case -5:
		case 10:
			keyHold[5] = false;
			return; // [i]
		case 48:
		case 49:
		case 50:
		case 51:
		case 52:
		case 53:
		case 54:
		case 55:
		case 56:
		case 57:
			keyHold[keyCode - 48] = false;
			return; // 0 -> 9
		case 42:
			keyHold[10] = false;
			return; // Key [*]
		case 35:
			keyHold[11] = false;
			return; // Key [#]
		case -6:
		case -21:
			keyHold[12] = false;
			return; // Soft1
		case -7:
		case -22:
			keyHold[13] = false;
			return; // Soft2
		}
	}
	//xÃ¡Â»Â­ lÃƒÂ½ Ã¡ÂºÂ¥n thÃ¡ÂºÂ£
	public void keyPressed(int keyCode) {

		mapKeyPress(keyCode);
	}

	protected void keyReleased(int keyCode) {

		mapKeyRelease(keyCode);
	}
	
	public static void clearKeyHold() {
		for (int i = 0; i < 14; i++)
			keyHold[i] = false;
	}

	public static void clearKeyPressed() {
        for (int i = 0; i < 14; i++)
            //keyPressed[i] = false;
            keyPressedz[i] = false;
	}
	
	
	

    //public void run() {
    //    // TODO Auto-generated method stub
    //    if (!isRunning) {
    //        isRunning = true;

    //    }
    //    try {
    //        while (isRunning) {

    //            long l1 = System.currentTimeMillis();
    //            input();
    //            update();
    //            repaint();
    //            serviceRepaints();

    //            while (System.currentTimeMillis() - l1 < delay)
    //                Thread.yield();
    //        }
    //    } catch (Exception e) {
    //        e.printStackTrace();
    //    }
    //    // Canvas.saveGame();
    //    Midlet.instance.notifyDestroyed();
		
    //}
	
	public static Image loadImage(String fileName) {
		Image img = null;
		try {
			img = Image.createImage(fileName);
		} catch (Exception e) {
		}
		if (img == null)
			Debug.Log("Load image '" + fileName + "' :ERORR ");
		return img;
	}

	public static int random(int a, int b) {
		return a + (r.nextInt(b - a));
	}

	public static bool testToDiffer(int x, int y, int w, int h, int x1,
			int y1, int w1, int h1) {
		return (x + w >= x1 && x <= x1 + w1 && y + h >= y1 && y <= y1 + h1);
	}

	public static int abs(int x) {
		return x > 0 ? x : -x;
	}

	public static bool isPointer(int x, int y, int w, int h) {
		if (!isPointerDown && !isPointerJustRelease)
			return false;
		if (px >= x && px <= x + w && py >= y && py <= y + h)
			return true;
		return false;
	}
	
	public static void clearPoint() {
		isPointerDown = isPointerClick = false;
	}

	public void pointerDragged(int x, int y) {
		px = x;
		py = y;
	}

	public void pointerPressed(int x, int y) {
		isPointerDown = true;
		px = x;
		py = y;

	}

	public void pointerReleased(int x, int y) {
		isPointerDown = false;
		isPointerClick = true;
		px = x;
		py = y;
	}

	public void input() {
		curScr.input();
	}

	public void update() {
		curScr.update();
	}

	public void paint(mGraphics g) {
		// TODO Auto-generated method stub
		curScr.paint(g);

		
	}

}


